﻿using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Cadre.Dto;
using NP.BPMReportPlatform.RecurringJobs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using static NP.BPMReportPlatform.Cadre.Enum.CadreEnum;
using static NP.BPMReportPlatform.Leave.Dto.SpecialApproverDto;

namespace NP.BPMReportPlatform.Cadre
{
    /// <summary>
    /// 干部转身接口
    /// </summary>
    //[Authorize]
    [ApiController]
    [Route("api/cadre")]
    public class CadreController : BPMReportPlatformController
    {
        private readonly ICadreService _cadreService;
        private readonly ICadreSyncJob _cadreEmailService;

        public CadreController(ICadreService cadreService, ICadreSyncJob cadreEmailService)
        {
            _cadreService = cadreService;
            _cadreEmailService = cadreEmailService;
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetListAsync")]
        public async Task<PagedResultDto<GetCadreResultOutput>> GetAllAsync([FromBody] GetCadreResultInput input)
        {
            return await _cadreService.GetCadreResult(input);
        }

        #region 干部转身列表，编辑
        /// <summary>
        /// 保存干部转身，准备启动...
        /// </summary>
        /// <param name="entity">数据</param>
        [HttpPost]
        [Route("Submit")]
        public Task<string> SubmitCadre([FromBody] GetCadreResultOutput entity)
        {
            return _cadreService.SubmitCadre(entity);
        }

        /// <summary>
        /// 干部转身信息批量导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportResultListAsync")]
        public async Task<XlsxFileResult<ExportCadreOutput>> ExportResultListAsync([FromBody] GetCadreResultInput input)
        {
            input.PageSize = 999;
            var rst = (await _cadreService.GetCadreResult(input)).Items.ToList();
            var result = rst.Adapt<List<ExportCadreOutput>>();
            return new XlsxFileResult<ExportCadreOutput>(result, "干部转身结果列表导出");
        }

        /// <summary>
        /// 单个数据导出
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("ExportResultDetailAsync")]
        public async Task<FileResult> ExportCadreResultAsync([FromQuery] string id)
        {
            var rst = await _cadreService.ExportCadreResultAsync(id);
            string downloadFileName = System.Guid.NewGuid().ToString("N") + ".xlsx";
            rst.Close();
            return File(rst.ToArray(), "application/octet-stream", $"{downloadFileName}.xlsx");
        }
        #endregion

        #region 导师辅导记录
        /// <summary>
        /// 保存导师辅导记录
        /// </summary>
        /// <param name="entity">数据</param>
        [HttpPost]
        [Route("TeacherRecord/Submit")]
        public async Task<string> SubmitTeacherRecord([FromBody] CadreTeacherRecordDto entity)
        {
            return await _cadreService.SubmitTeacherRecord(entity);
        }

        /// <summary>
        /// 保存述职总结结果
        /// </summary>
        /// <param name="input">数据</param>
        [HttpPost]
        [Route("CadreResult/Submit")]
        public async Task<string> SubmitCadreResult(CadreResultDto input)
        {
            return await _cadreService.SubmitCadreResult(input);
        }

        /// <summary>
        /// 获取干部转身所有信息
        /// </summary>
        /// <param name="cadreId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("CadreDetail")]
        public async Task<CadreDetailDto> GetCadreDetail([FromQuery] string cadreId)
        {
            return await _cadreService.GetCadreDetail(cadreId);
        }
        #endregion

        /// <summary>
        /// 获取新员工
        /// </summary>
        /// <param name="workNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("UserDetail")]
        public async Task<object> GetUserDetail([FromQuery] string workNumber)
        {
            return await _cadreService.GetUserDetail(workNumber);
        }

        /// <summary>
        /// 发送邮件通知
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cadreEmailEnum"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SendCadreEmail")]
        public void SendCadreEmail(string id, CadreEmailEnum cadreEmailEnum)
        {
            _cadreEmailService.SendCadreEmail(cadreEmailEnum, id);
        }

        #region 报表
        /// <summary>
        /// 导师培训次数统计
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Report/CadreListAsync")]
        public async Task<object> GetReportTeacherAsync([FromBody] CadreReportSearchDto input)
        {

            if (input.ReportType == 1)
            {
                // 导师培训次数统计
                return await _cadreService.GetReportTeacher(input);
            }
            else if (input.ReportType == 2)
            {
                // 转身执行统计表
                return await _cadreService.GetReportCadreOrgCount(input);
            }
            else if (input.ReportType == 3)
            {
                // 干部上岗前沟通记录表
                return await _cadreService.GetReportCadreRecord(input);
            }
            else if (input.ReportType == 4)
            {
                // 导师辅导记录表
                return await _cadreService.GetReportCadreTeacherRecord(input);
            }
            else if (input.ReportType == 5)
            {
                // 转身述职记录表
                return await _cadreService.GetReportCadreResult(input);
            }
            else if (input.ReportType == 6)
            {
                // 转身计划总结记录表
                return await _cadreService.GetReportProcess(input);
            }

            return null;
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Report/ExportCadreListAsync")]
        public async Task<object> ExportReportTeacherAsync([FromBody] CadreReportSearchDto input)
        {
            input.PageSize = int.MaxValue;
            input.IsExport = true;
            // 导师培训次数统计
            if (input.ReportType == 1)
            {
                var rst = (await _cadreService.GetReportTeacher(input)).Items.ToList();
                return new XlsxFileResult<CadreReportTeacherOutPut>(rst, "导师培训次数统计报表");
            }
            else if (input.ReportType == 2)
            {
                // 转身执行统计表
                var rst = await _cadreService.GetReportCadreOrgCount(input);
                return new XlsxFileResult<CadreReportOrgCountOutPut>(rst, "转身执行统计表");
            }
            else if (input.ReportType == 3)
            {
                // 干部上岗前沟通记录表
                var rst = await _cadreService.GetReportCadreRecord(input);
                return new XlsxFileResult<CadreReportRecord>(rst.Items.ToList(), "干部上岗前沟通记录表");
            }
            else if (input.ReportType == 4)
            {
                // 导师辅导记录表
                var rst = await _cadreService.GetReportCadreTeacherRecord(input);
                return new XlsxFileResult<CadreReportTeacherRecord>(rst.Items.ToList(), "导师辅导记录表");
            }
            else if (input.ReportType == 5)
            {
                // 转身述职记录表
                var rst = await _cadreService.GetReportCadreResult(input);
                return new XlsxFileResult<CadreReportResult>(rst.Items.ToList(), "转身述职记录表");
            }
            else if (input.ReportType == 6)
            {
                // 转身计划总结记录表
                var rst = await _cadreService.GetReportProcess(input);
                return new XlsxFileResult<CadreReportProcess>(rst.Items.ToList(), "转身计划总结记录表");
            }
            return null;
        }
        #endregion
    }
}